from django.db import models
from django import template
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic


#from ModuleDeliberation.models import Comment
from django.utils.translation import ugettext as _ 
from pirate_core.views import template_for_model


class Comment(models.Model):
    user = models.ForeignKey(User)
    submit_date = models.DateTimeField('date_published')
    text = models.TextField(max_length=1200)
    content_type = models.ForeignKey(ContentType,
                                      verbose_name=_('content type'),
                                      related_name="content_type_set_for_pirate_%(class)s")
    object_pk = models.IntegerField(_('object ID'))
    content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
    reply_to = models.ForeignKey('self', related_name="comment_parent",null=True)
    is_leaf = models.BooleanField()
    is_root = models.BooleanField()

    class Meta:
        verbose_name = _('comment')
    
    def __unicode__(self):
        return self.user.username + ":" + str(self.submit_date)

    def get_absolute_url(self):
        content_type = self.content_type
        path = template_for_model(str(content_type)) + "?_t=" + str(content_type.pk) + "&_o=" + str(self.object_pk)  +"&_c=comment" + str(self.id)
        return path


admin.site.register(Comment)

